package com.sky.service.impl;

import com.fasterxml.jackson.databind.util.BeanUtil;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    @Transactional
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //判断当前商品是否存在
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list=shoppingCartMapper.listShoppingCart(shoppingCart);
        //存在，数量++
        if(list!=null&&!list.isEmpty()){
            ShoppingCart item=list.get(0); //由userId和setmealId/（dishId+dishFlavor）定位，结果必为1
            item.setNumber(item.getNumber()+1);
            shoppingCartMapper.update(item);
        }else{
            //不存在，新建添加
            if(shoppingCart.getSetmealId()!=null){
                //添加的是菜品
                Setmeal setmeal= setmealMapper.selectById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }else{
                Dish dish=dishMapper.selectDishById(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }


    /**
     * 列出购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart=ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        return shoppingCartMapper.listShoppingCart(shoppingCart);
    }

    /**
     * 删减购物车
     * @param shoppingCartDTO
     */
    @Override
    @Transactional
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list=shoppingCartMapper.listShoppingCart(shoppingCart);
        if(list!=null&&!list.isEmpty()){
            shoppingCart=list.get(0);
            if(shoppingCart.getNumber()<=1){
                //清除该项
                shoppingCartMapper.delete(shoppingCart);
            }else{
                //更新该项
                shoppingCart.setNumber(shoppingCart.getNumber()-1);
                shoppingCartMapper.update(shoppingCart);
            }
        }
    }

    /**
     * 清空购物车
     */
    @Override
    @Transactional
    public void cleanShoppingCart() {
        ShoppingCart shoppingCart=ShoppingCart.builder()
                        .userId(BaseContext.getCurrentId())
                                .build();
        shoppingCartMapper.delete(shoppingCart);
    }
}
